package com.nuedu.corejava.ch14;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashSet;

import java.util.Set;

public class ChatRoomServer {
	private Set<Socket> clientSet= new HashSet<Socket>();
	
	public void start(int port) {
		
		ServerSocket serverSocket = null;
		  try {
			serverSocket = new ServerSocket(port);
			System.out.println("服务启动成功！");
			while(true) {
			   Socket socket =  serverSocket.accept();
			   System.out.println("客户端接入成功:"+socket.getInetAddress().getHostAddress()+" : "+socket.getPort());
			    clientSet.add(socket);
			   new ChatRoom(socket, clientSet).start();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	

	public static void main(String[] args) {
		
		new  ChatRoomServer().start(8888);
	}

}

